<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      /* 方法一 */
      /* article {
              width: 400px;
              height: 400px;
              border: solid 2px blueviolet;
            }
            div {
              width: 200px;
              height: 200px;
              background: orange;
              margin: 0 auto;
              margin-top: 100px;
            } */

      /* 方法二 */
      /* article {
              position: relative;
              width: 400px;
              height: 400px;
              border: solid 2px blueviolet;
            } */
      /* div {
              width: 200px;
              height: 200px;
              background: orange;
              position: absolute;
              top: 50%;
              left: 50%;
              margin-top: -100px;
              margin-left: -100px;
            } */
      /* 等同于 margin-top: -100px; margin-left: -100px
              transform: translate(-100px, -100px); */
      /* 优化代码 */
      /* div {
              width: 200px;
              height: 200px;
              background: orange;
              position: absolute;
              top: calc(50% - 100px);
              left: calc(50% - 100px);
      `      } */

      /* 方法三 */
      /* 3. 使用绝对定位并margin自适应进行居中 */
      /* article {
              position: relative;
              width: 400px;
              height: 400px;
              background: red;
            }
            div {
              width: 200px;
              height: 200px;
              background: yellow;
              position: absolute;
              left: 0;
              right: 0;
              top: 0;
              bottom: 0;
              margin: auto;
            } */

      /* 使用table-cell 进行居中显示 */
      /* 可以实现垂直居中 */
      /* article {
        width: 400px;
        height: 400px;
        background: red;
        display: table-cell;
        vertical-align: middle;
      } */
      /* 可以实现水平居中 */
      /* div {
        width: 200px;
        height: 200px;
        background: yellow;

        margin: 0 auto;
      } */

      article {
        width: 400px;
        height: 400px;
        background: blue;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      div {
        width: 200px;
        height: 200px;
        background: red;
      }
    </style>
  </head>
  <body>
    <!-- 居中定位的五种方法 -->
    <article>
      <div></div>
    </article>
  </body>
</html>
